package com.epoint.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.epoint.common.Result;
import com.epoint.entity.ProductInfo;
import com.epoint.service.ProductInfoServicce;

/**
 * Servlet implementation class ProductInfolistAction
 */
@WebServlet("/productinfoaction")
public class ProductInfoAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProductInfoServicce productInfoService = new ProductInfoServicce();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProductInfoAction() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		PrintWriter out = response.getWriter();

		if ("getProductInfo".equals(method)) {
			// 根据Id获取产品信息
			String productId = request.getParameter("productId");
			out.println(JSONObject.toJSONString(productInfoService.getProductInfo(productId)));
		} else if ("getAllProducts".equals(method)) {
			List<ProductInfo> allProducts = productInfoService.getAllProducts();
			out.print(JSONObject.toJSONStringWithDateFormat(allProducts, "yyyy-MM-dd"));
		} else if ("getNextId".equals(method)) {
			// 获取下一个产品编号
			out.println(JSONObject.toJSONString(productInfoService.getNextProductId()));
		} else {
			String productName = request.getParameter("productName");
			String productUsage = request.getParameter("productUsage");
			String pageIndex = request.getParameter("pageIndex");
			String pageSize = request.getParameter("pageSize");
			String sortField = request.getParameter("sortField");
			String sortOrder = request.getParameter("sortOrder");

			List<ProductInfo> productList = productInfoService.getProductList(productName, productUsage,
					Integer.valueOf(pageIndex), Integer.valueOf(pageSize), sortField, sortOrder);
			int count = productInfoService.getProductListCount(productName, productUsage);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("total", count);
			resultMap.put("data", productList);
			// 返回构建的json数据
			out.println(JSONObject.toJSONStringWithDateFormat(resultMap, "yyyy-MM-dd"));
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		PrintWriter out = response.getWriter();

		if ("del".equals(method)) {
			String data = request.getParameter("data");
			String[] productIds = data.split(",");

			List<String> successDel = new ArrayList<>();
			List<String> failDel = new ArrayList<>();

			for (String productId : productIds) {
				String productName = productInfoService.findById(productId).getProductName();

				if (productInfoService.deleteProductByProductId(productId) > 0) {
					successDel.add(productName);
				} else {
					failDel.add(productName);
				}
			}
			// XX 删除成功；YY 删除失败
			StringBuilder msgSb = new StringBuilder();
			for (int i = 0; i < successDel.size(); i++) {
				msgSb.append(successDel.get(i) + "删除成功;");
			}
			for (int i = 0; i < failDel.size(); i++) {
				msgSb.append(failDel.get(i) + "删除失败");
			}
			String returnCode = Result.returnCode(4, msgSb.toString());
			response.getWriter().print(returnCode);

		} else if ("add".equals(method)) {
			String data = request.getParameter("data");
			ProductInfo productInfo = JSONObject.parseObject(data, ProductInfo.class);

			// 最新报价默认为0、报价次数默认为0
			productInfo.setQuote(new BigDecimal(0));
			productInfo.setCount(0);
			int flag = productInfoService.addProductInfo(productInfo);

			String returnCode = Result.returnCode(flag);
			out.print(returnCode);
		} else if ("update".equals(method)) {
			String data = request.getParameter("data");
			ProductInfo productInfo = JSONObject.parseObject(data, ProductInfo.class);
			int res = productInfoService.updateProductInfo(productInfo);
			
			String returnCode = Result.returnCode(res);
			response.getWriter().print(returnCode);
		}
	}

}
